<template>
  <el-form-item class="multi-item-list">
    <el-row slot="label" type="flex" justify="space-between" align="middle">
      <span>{{label}}</span>
      <el-button class="add-btn" type="text"
        icon="el-icon-circle-plus"
        :disabled="disabled || maxCount <= (dataList || []).length"
        @click.stop="onEditItem()"
      >
        添加
      </el-button>
    </el-row>
    <!--
    <div class="item" v-for="item in dataList" :key="item[prop.value]">
      <el-row slot="title" type="flex" align="middle" justify="space-between" style="width: 100%;">
        <el-link type="itemType" :disabled="disabled" @click="onEditItem(item)">
          <slot v-if="hasSlot" :data="item" />
          <span v-else>{{item[prop.label]}}</span>
        </el-link>
        <div class="right">
          <slot name="right" :data="item" />
          <i class="el-icon-close" @click.stop="onDeleteItem(item)" />
        </div>
      </el-row>
    </div>
    -->
    <Draggable draggable=".list-item" :list="dataList" :group="dragGroup"
      style="overflow: hidden; height: 100%;" :disabled="!supportSort"
    >
      <el-row class="list-item" v-for="item in dataList" :key="item[prop.value]"
        type="flex" align="middle" justify="space-between" style="width: 100%;"
      >
        <div class="item" style="padding: 0px 15px; width: 100%;">
          <el-row type="flex" align="middle" justify="space-between" style="padding: 6px 0px; min-height: 32px;">
            <el-link style="line-height: 20px; color: #666666; font-size: 12px;"
              :disabled="disabled"
              @click="onEditItem(item)"
            >
              <slot v-if="hasSlot" :data="item" />
              <span v-else>{{item[prop.label]}}</span>
            </el-link>
            <div class="right" style="line-height: 20px; font-size: 12px;">
              <slot name="right" :data="item" />
            </div>
          </el-row>
        </div>
        <i class="close-btn el-icon-remove-outline" style="margin-left: 8px;" @click.stop="onDeleteItem(item)" />
      </el-row>
    </Draggable>
  </el-form-item>
</template>

<script>
import Draggable from 'vuedraggable';

export default {
  name: 'multiItemList',
  props: {
    label: {
      type: String,
      required: true
    },
    /**
     * 数据
     */
    data: {
      type: [Array, Object]
    },
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 最大项数，当达到最大项数，则不能添加数据
     */
    maxCount: {
      type: Number
    },
    /**
     * 是否支持拖拽排序
     */
    supportSort: {
      type: Boolean,
      default: false
    },
    dragGroup: {
      type: String,
      default: 'componentsGroup'
    },
    prop: {
      type: Object,
      default: () => {
        return {
          // 数据显示字段
          label: 'name',
          // 数据值字段
          value: 'id'
        }
      }
    }
  },
  components: {
    Draggable
  },
  computed: {
    hasSlot () {
      return this.$scopedSlots.default;
    },
    dataList () {
      if (this.data == null) return [];
      return Array.isArray(this.data) ? this.data : [this.data];
    }
  },
  methods: {
    onEditItem (item) {
      if (this.disabled) return;
      if (item == null) {
        this.$emit('add');
      } else {
        this.$emit('edit', item);
      }
    },
    onDeleteItem (item) {
      if (this.disabled) return;
      this.$emit('delete', item);
    }
  }
}
</script>

<style scoped>
  .multi-item-list >>> .el-form-item__label {
    width: 100%;
  }

  .multi-item-list .item {
    border-radius: 4px;
    border: 1px solid #E8E8E8;
    background: white;
  }

  .list-item + .list-item {
    margin-top: 8px;
  }
</style>

<style lang="scss" scoped>
  @import '@/assets/style/element-variables.scss';

  .add-btn {
    font-size: 12px;
    margin-left: 5px;
    color: $--color-primary;
  }

  .close-btn {
    cursor: pointer;
  }
</style>
